﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Microsoft.Expression.Interactivity.Core;
using WebSite.ClientBin.Config;

namespace QDP.Platform.Admin.ViewModels
{
    public class MainViewModel:INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
        private Settings Settings { get; set; }
        private XmlSerializer serializer = new XmlSerializer(typeof(Settings));
        private XDocument doc;
        private ObservableCollection<KeyValueItem> sets;
        private ObservableCollection<FileItem> files;
        private bool isBusy;
        public ObservableCollection<KeyValueItem> Sets
        {
            get { return sets; }
            set
            {
                if (sets != value)
                {
                    sets = value;
                    NotifyPropertyChanged( "Sets");
                }
            }
        }
        public ObservableCollection<FileItem> Files
        {
            get { return files; }
            set
            {
                if (files != value)
                {
                    files = value;
                    NotifyPropertyChanged("Files");
                }
            }
        }
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                if (isBusy != value)
                {
                    isBusy = value;
                    NotifyPropertyChanged("IsBusy");
                }
            }
        }
        public ICommand SaveCommand { get; private set; }
        public ICommand AddSetCommand { get; private set; }
        public ICommand AddUrlCommand { get; private set; }
        public ICommand RefreshCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }
        public ICommand DeleteUrlCommand { get; private set; }

        public MainViewModel()
        {
            SaveCommand = new ActionCommand(Save);
            AddSetCommand = new ActionCommand(AddSet);
            AddUrlCommand = new ActionCommand(AddUrl);
            RefreshCommand = new ActionCommand(Refresh);
            DeleteCommand = new ActionCommand(Delete);
            DeleteUrlCommand = new ActionCommand(DeleteUrl);

            this.Refresh();
        }
        private void DeleteUrl(object selected)
        {
            Files.Remove(selected as FileItem);
        }
        private void Delete(object selected)
        {
            Sets.Remove(selected as KeyValueItem);
        }
        private void AddSet()
        {
            Sets.Add(new KeyValueItem());
        }
        private void AddUrl()
        {
            Files.Add(new FileItem());
        }
        private void Refresh()
        {
            try
            {
                IsBusy = true;
                WebClientOperater.Read("/clientbin/config/settings.xml", (s, r) =>
                {
                    IsBusy = false;
                    if (s)
                    {
                        Serializer(r);
                    }
                    else
                    {
                        MessageBox.Show(r);
                    }
                });
            }
            catch { IsBusy = false; }
        }
        private void Save()
        {
            IsBusy = true;
            Settings.Files = Files.ToList();
            Settings.Sets = Sets.ToList();
            StringBuilder builder = new StringBuilder();
            StringWriter writer = new StringWriter(builder);
            serializer.Serialize(writer, Settings);
            string content = builder.ToString();
            writer.Close();
            WebClientOperater.Write("clientbin/config/default.ashx", content, (s, r) =>
            {
                IsBusy = false ;
                if (s == false)
                {
                    MessageBox.Show(r);
                }
            });
        }
        private void Serializer(string xml)
        {
            IsBusy = true;
            doc = XDocument.Parse(xml);

            XmlReader reader = doc.CreateReader();
            if (serializer.CanDeserialize(reader))
            {
                Settings = serializer.Deserialize(reader) as Settings;
                Sets = new ObservableCollection<KeyValueItem>();
                Files = new ObservableCollection<FileItem>();
                foreach (var v in Settings.Sets)
                {
                    Sets.Add(v);
                }
                foreach (var v in Settings.Files)
                {
                    Files.Add(v);
                }
            }
            else
            {
                Settings = null;
            }
            IsBusy = false;
        }
        
    }
}